//
//  AccountViewController.m
//  Finance
//
//  Created by cheng xiang on 13-4-2.
//  Copyright (c) 2013年 cheng xiang. All rights reserved.
//

#import "AccountViewController.h"
#import "AccountLoginViewController.h"
#import "AccountRegisterViewController.h"
#import "UserBuyProductViewController.h"
#import "UserProductViewController.h"
#import "AppDelegate.h"
#import <QuartzCore/QuartzCore.h>
@interface AccountViewController ()

@end

@implementation AccountViewController
@synthesize accountTable;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

//- (void)viewWillAppear:(BOOL)animated
//{
//    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//    UITabBarController *aController = (UITabBarController *)delegate.window.rootViewController;
//    [delegate showTabBar:aController];
//    [super viewWillAppear:animated];
//}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"toolbar.jpg"] forBarMetrics:UIBarMetricsDefault];
    }
    CGRect winSize = self.view.frame;
     UIColor* bk = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"beij.png"]];
    self.accountTable  = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, winSize.size.width, winSize.size.height) style:UITableViewStyleGrouped];
    
    self.accountTable.dataSource = self;
    self.accountTable.delegate = self;
    self.accountTable.backgroundView = nil;
    [self.accountTable setBackgroundColor:bk];
    [self.view addSubview:self.accountTable];
    

    AppDelegate* mydelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate];
    if(mydelegate.loginName != nil)
    {
        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"注销" style:UIBarButtonItemStyleBordered target:self action:@selector(UnLogin)];
        [leftItem setBackgroundImage:[UIImage imageNamed:@"btn.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        [leftItem setBackgroundImage:[UIImage imageNamed:@"btn.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        
        [self.navigationItem setRightBarButtonItem:leftItem];
    }
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(goReturn)];
    [rightItem setBackgroundImage:[UIImage imageNamed:@"btn.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [rightItem setBackgroundImage:[UIImage imageNamed:@"btn.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    self.navigationItem.leftBarButtonItem = rightItem;
    
}

- (void)viewDidAppear:(BOOL)animated
{
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    UITabBarController *aController = (UITabBarController *)delegate.window.rootViewController;
    [delegate hideTabBarWithoutAnimation:aController];
}

- (void)goReturn
{
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    UITabBarController *aController = (UITabBarController *)delegate.window.rootViewController;
    [delegate showTabBarWithoutAnimation:aController];
    [self.navigationController popViewControllerAnimated:YES];
    
}

-(void)UnLogin
{
    AppDelegate* mydelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate];
    [mydelegate writeplist:@"username" PlistValue:@""];
    [mydelegate writeplist:@"password" PlistValue:@""];
    [mydelegate writeplist:@"islogin" PlistValue:false];
    
    mydelegate.loginName = nil;
    mydelegate.loginSessionID = nil;
    
    [self showMessageBox:@"提示" content:@"注销成功"];
}

-(void)viewWillDisappear:(BOOL)animated {
    
    //[self resignFirstResponder];
    [self setHidesBottomBarWhenPushed:NO];//隐藏uitabbar需要这样调用
    [super viewWillDisappear:animated];
    
}

#pragma - 
#pragma table function
#pragma -
#pragma table property

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return  3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 2;
    }

    return  1;
}


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* strIdentity = @"customtableviewcell";
    
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:strIdentity];
    
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strIdentity];
    }
    
    if ([indexPath section] == 0)
    {
        if ([indexPath row]  == 0)
        {
            cell.textLabel.text = @"登录";
        }
        else if(indexPath.row == 1)
        {
            cell.textLabel.text = @"注册";
        }
    }
    else if([indexPath section] == 1)
    {
        cell.textLabel.text = @"用户关注产品";
    }
    else if ([indexPath section] == 2)
    {
         cell.textLabel.text =  @"用户已购产品";
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize winsize = self.view.bounds.size;
    if ([indexPath section] == 0)
    {
        if([indexPath row] == 0)        //登录
        {
            AppDelegate* mydelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate];
        
            if(mydelegate.loginSessionID != nil)
            {
                [self showMessageBox:@"登录提示" content:@"用户已登录"];
                return;
            }
            
            [self setHidesBottomBarWhenPushed:YES];

            accountLogin = [[AccountLoginViewController alloc] initWithNibName:@"AccountLoginViewController" bundle:nil];
            accountLogin.view.frame = CGRectMake(0, -2,winsize.width,winsize.height);
            
            [self.navigationController pushViewController:accountLogin animated:YES];
        }
        else if([indexPath row] == 1)   //注册
        {
            AppDelegate* mydelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate];
            
            if(mydelegate.loginSessionID != nil)
            {
                [self showMessageBox:@"登录提示" content:@"用户已登录"];
                return;
            }
            
            [self setHidesBottomBarWhenPushed:YES];
            
            accountRegister = [[AccountRegisterViewController alloc] initWithNibName:@"AccountRegisterViewController" bundle:nil];
            accountRegister.view.frame = CGRectMake(0, -2,winsize.width,winsize.height);

            [self.navigationController pushViewController:accountRegister animated:YES];
            
        }
    }
    else if([indexPath section] == 1)
    {
        if ([indexPath row] == 0)
        {
            AppDelegate* mydelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate];
            
            if(mydelegate.loginSessionID == nil)
            {
                [self showMessageBox:@"提示" content:@"用户没有登录"];
                return;
            }
            
            [self setHidesBottomBarWhenPushed:YES];
            userProduct = [[UserProductViewController alloc] init];
            
            userProduct.view.frame = CGRectMake(0, -2,winsize.width,winsize.height);
      
            [self.navigationController pushViewController:userProduct animated:YES];
        }
    }
    else if([indexPath section] == 2)
    {
        if ([indexPath row] == 0)
        {
            AppDelegate* mydelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate];
            
            if(mydelegate.loginSessionID == nil)
            {
                [self showMessageBox:@"提示" content:@"用户没有登录"];
                return;
            }
            
            [self setHidesBottomBarWhenPushed:YES];
            userBuy = [[UserBuyProductViewController alloc] init];
            userBuy.view.frame = CGRectMake(0, -2,winsize.width,winsize.height);
    
            [self.navigationController pushViewController:userBuy animated:YES];
        }
    }
}

-(void)showMessageBox:(NSString*)title content:(NSString*)content
{
    UIAlertView* msg = [[UIAlertView alloc] initWithTitle:title message:content delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [msg show];
}

#pragma -
#pragma sys function

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated.
}

@end
